package com.notepad.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.EditText;

public class LinedEditText extends EditText {
	private int lineColor;
	private float lineWidth;

	public LinedEditText(Context context) {
		super(context);
		lineColor = Color.GRAY;
		lineWidth = 1.0f;
	}

	public LinedEditText(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		lineColor = Color.GRAY;
		lineWidth = 1.0f;
	}

	public LinedEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		lineColor = Color.GRAY;
		lineWidth = 1.0f;
	}

	public LinedEditText(Context context, int lineColor, float lineWidth) {
		super(context);
		this.lineColor = lineColor;
		this.lineWidth = lineWidth;
	}

	public int getLineColor() {
		return lineColor;
	}

	public void setLineColor(int lineColor) {
		this.lineColor = lineColor;
	}

	public float getLineWidth() {
		return lineWidth;
	}

	public void setLineWidth(float lineWidth) {
		this.lineWidth = lineWidth;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		// 创建画笔
		Paint mPaint = new Paint();
		mPaint.setColor(lineColor);
		mPaint.setStrokeWidth(lineWidth);
		mPaint.setStyle(Paint.Style.FILL);

		// 获取控件 左、右、顶部留白
		int padL = this.getPaddingLeft();
		int padR = this.getPaddingRight();
		int padT = this.getPaddingTop();

		int lines = getLineCount();// 行数
		int size = (int) getTextSize(); // 字体大小
		int baseTop = padT + size / 6;//size/6，近似于行距的一半。测试发现行距的值近似于TextSize的1/3。
		int lineHeight = this.getLineHeight(); // 行宽

		// 画线
		for (int i = 1; i < lines; i++) {
			canvas.drawLine(padL, 
					baseTop + lineHeight * i,
					this.getWidth()- padR, 
					baseTop + lineHeight * i,
					mPaint);
		}

	}

}
